package com.snj.visangedu.listeningmax;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;

import com.snj.visangedu.R;

public class FolderFragment extends Fragment implements OnClickListener {
	private final String TAG = "FolderFragment";
	
	ListView lstv_folders = null;	
	FolderAdapter _adapter = null;
	ArrayList<FolderItem> mFolderList = new ArrayList<FolderItem>();
		
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View root = inflater.inflate(R.layout.fragment_listeningmax_folder,
				container, false);
		return root;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated");
		
		super.onActivityCreated(savedInstanceState);
		init_widget();
	}

	private void init_widget() {		
		View view = getView();
		
		lstv_folders = (ListView)view.findViewById(R.id.lstv_folders);
		
		_adapter = new FolderAdapter(getActivity(), R.layout.folderitem, mFolderList);
		
		lstv_folders.setAdapter(_adapter);
		GetMediaList();
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onClick(View v) {		
		switch(v.getId())
		{
		case R.id.img_studyplanner_month:
			break;
		}
	}
	
	
	private void GetMediaList()
	{
		
		HashMap<String, ArrayList<MediaItem>> mediamap = new HashMap<String, ArrayList<MediaItem>>();
		
		String[] proj = {
				MediaStore.Audio.AudioColumns.ALBUM
				, MediaStore.Audio.Media._ID
				, MediaStore.Audio.Media.TITLE
				, MediaStore.Audio.Media.DATA
				, MediaStore.Audio.Media.DISPLAY_NAME
				, MediaStore.Audio.Media.SIZE
				, MediaStore.Audio.Media.DATE_ADDED};
		
		Cursor c = getActivity().managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
		
		if(c.moveToFirst())
		{
			int n_album = c.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);
			int n_id = c.getColumnIndex(MediaStore.Audio.Media._ID);
			int n_title = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
			int n_data= c.getColumnIndex(MediaStore.Audio.Media.DATA);
			int n_displayname = c.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);
			int n_size = c.getColumnIndex(MediaStore.Audio.Media.SIZE);
			int n_regdate = c.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED);
			
			do
			{								
				String path = c.getString(n_data);
				
				if(!path.endsWith(".mp3") && !path.endsWith(".wma"))
				{
					continue;
				}
				
				String foldername = "";
				String filename = "";
				
				filename = path.substring(path.lastIndexOf("/") + 1);
				
				path = path.substring(0, path.lastIndexOf("/"));
				foldername = path.substring(path.lastIndexOf("/") + 1);
				
				if(!mediamap.containsKey(path))
				{
					mediamap.put(path, new ArrayList<MediaItem>());
				}
				
				mediamap.get(path).add(new MediaItem(filename, path));
				
				
			}
			while(c.moveToNext());
			
			Set keys = mediamap.keySet();
			
			for(Iterator iterator = keys.iterator(); iterator.hasNext();) {
				//Log.d(TAG, "key : " + iterator.next());
				String folderpath = (String)iterator.next();
				String foldername = folderpath.substring(folderpath.lastIndexOf("/") + 1);
				FolderItem folderitem = new FolderItem(foldername, folderpath, mediamap.get(folderpath));
				
				mFolderList.add(folderitem);
			}
			
			_adapter.notifyDataSetChanged();
			
			
		}
		
		c.close();
	}
	
	
}

